<?php
/**
 * app首页控制器
 * @DateTime 2017-09-07
 * @author xyl
 */
class Index extends App_Controller{
	
	function __construct(){
		parent::__construct();
		//加载用户户模型
		$this->load->model('Information_model','infos_mdl');
		$this->load->model('Introduce_model','introduce_mdl');
		$this->load->model('Video_model','video_mdl');
		$this->load->model('Step_model','step_mdl');
	}

	/**
	 * app首页
	 * @DateTime 2017-09-07
	 * @author xyl
	 * @return   [type]     [description]
	 */
	public function index(){
		$where['status'] = 1;
		//获取轮播的消息
		$infos = $this->infos_mdl->ajaxGetInfosList(5,0,$where,'id,theme');
		//获取轮播视频
		$video = $this->video_mdl->getVideoDetail(array('id'=>1));
		//获取日排行和周排行
		$data = $this->get_rank();
		$return = array(
			'code' 		=> 	1,
			'msg'  		=> 	'请求成功',
			'video'	=>	$video['link'],
			'info' 	=> 	$infos['data'],
		);
		$return = array_merge($return,$data);
		echo json_encode($return);return;
	}

	/**
	 * 获取排行榜
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @return   [type]     [description]
	 */
	public function get_rank($type = 0,$page = 1)
	{
		$data = array();
		//日排行榜
		if ($type==1 || $type==0) {
			$data['day_rank'] = $this->step_mdl->get_totay_rank($page);
		}
		if ($type==2 || $type==0) {
			$data['week_rank'] = $this->step_mdl->get_week_rank($page);
		}
		return $data;
	}

	/**
	 * 获取排行榜
	 * @author XYL
	 * @DateTime 2018-06-04
	 * @return   [type]     [description]
	 */
	public function get_rank_list()
	{
		$post = file_get_contents("php://input");

		$post = json_decode($post,true);
		$data = $this->get_rank($post['type'],isset($post['page'])?$post['page']:1);
		$return = array(
			'code' 		=> 	1,
			'msg'  		=> 	'请求成功',
		);
		$return = array_merge($return,$data);
		echo json_encode($return);return;
	}

	/**
	 * 点击资讯详情
	 * @DateTime 2017-09-07
	 * @author xyl
	 * @param  [type]  $id      [资讯id]
	 * @param  integer $type    [进入类型（0滚动消息进入，1介绍进入，2滚动消息的关联信息，3介绍的关联信息）]
	 * @param  boolean $main_id [description]
	 * @return [type]           [description]
	 */
	public function getInfoDetail($id,$type = 0,$main_id = false)
	{
		if ($type==0) {
			//点击滚动消息进入
			$vdata = $this->infos_mdl->getInfoDetail(array('id'=>$id),1);
			if (isset($vdata['relation']) && !empty($vdata['relation'])) {
				foreach ($vdata['relation'] as $key => &$value) {
					$value['url'] = base_url().'App/Index/getInfoDetail/'.$value['id'].'/2/'.$vdata['info']['id'];
				}
			}
		} elseif ($type==1) {
			//点击介绍进入
			$vdata = $this->introduce_mdl->getIntroduceDetail($id);
			if (isset($vdata['relation']) && !empty($vdata['relation'])) {
				foreach ($vdata['relation'] as $key => &$value) {
					$value['url'] = base_url().'App/Index/getInfoDetail/'.$value['id'].'/3/'.$id;
				}
			}
		} elseif ($type==2) {
			//点击轮播图的关联信息
			//获取轮播图的信息
			$data = $this->infos_mdl->getInfoDetail(array('id'=>$main_id),1);
			$relation = array();	
			foreach ($data['relation'] as $key => $value) {
				if ($value['id']==$id) {
					$vdata['info'] = $value;
				}else{
					$value['url'] = base_url().'App/Index/getInfoDetail/'.$value['id'].'/2/'.$main_id;
					$relation[] =$value;
				}
			}
			//将轮播图信息存入关联信息中
			$data['info']['url'] = base_url().'App/Index/getInfoDetail/'.$main_id;
			$relation[] = $data['info'];
			$vdata['relation'] = $relation;
		} else{
			//点击介绍的关联信息
			//获取关联信息
			$data = $this->introduce_mdl->getIntroduceDetail($main_id);
			// print_r($data);
			$relation = array();
			foreach ($data['relation'] as $key => $value) {
				if ($value['id']==$id) {
					$vdata['info'] = $value;
				}else{
					$value['url'] = base_url().'App/Index/getInfoDetail/'.$value['id'].'/3/'.$main_id;
					$relation[] =$value;
				}
			}
			//将介绍信息存入关联信息中
			$data['info']['url'] = base_url().'App/Index/getInfoDetail/'.$main_id.'/1';
			$relation[] = $data['info'];
			$vdata['relation'] = $relation;
		}
		$vdata['type'] = $type;
		// print_r($vdata);die;
		$this->load->view('weixin/androidInformation',$vdata);
	}

	/**
	 * 获取扫码的队列
	 * @author XYL
	 * @DateTime 2018-06-06
	 * @return   [type]     [description]
	 */
	public function get_qr_list()
	{
		$this->load->model('QRList_model','qrlist_mdl');
		$info = $this->qrlist_mdl->get_last_user();
		$return = array(
			'code'	=>	(!empty($info)?1:0),
			'msg'	=>	(!empty($info)?'获取扫码人员信息成功':'当前没有人扫码'),
		);
		echo json_encode($return);return;
	}

	/**
	 * 安卓分享
	 * @author XYL
	 * @DateTime 2018-06-06
	 * @return   [type]     [description]
	 */
	public function share()
	{
		//获取当前队列最后一个
		$this->load->model('QRList_model','qrlist_mdl');
		$info = $this->qrlist_mdl->get_last_user();
		//清除队列
		$this->qrlist_mdl->remove_list($info['id']);
		$this->load->model('User_model', 'user_mdl');
		$this->load->model('Step_model','step_mdl');
		$data['info'] = $this->user_mdl->getUser(array('id'=>$info['user_id']));
		$data['step'] = $this->step_mdl->get_today($info['user_id']);
		// print_r($data['step']);
		// $sort = $this->step_mdl->get_sort($info['user_id']);
		// $data = array_merge($data,$sort);
		$this->load->view('weixin/androidShare',$data);
	}

	/**
	 * 生成二维码
	 * @author XYL
	 * @DateTime 2018-06-06
	 */
	public function QRcode()
	{
		error_reporting(E_ERROR);
        require_once 'phpqrcode.php';
        $filename = './static/img/qrcode.png';
        QRcode::png(base_url().'Weixin/Index/share',$filename,QR_ECLEVEL_L,6);
	}

	/**
	 * 排行榜
	 * @author XYL
	 * @DateTime 2018-06-08
	 * @param    integer    $type [description]
	 * @return   [type]           [description]
	 */
	public function rank_list($type = 1)
	{
		$this->load->model('Step_model','step_mdl');
		if ($type==1) {
			$list = $this->step_mdl->get_totay_rank();
		} else {
			$list = $this->step_mdl->get_week_rank();

		}
		$data['list'] = $list;
		$data['type'] = $type;
		$this->load->view('weixin/rank',$data);
	}

	/**
	 * 生成假数据
	 * @author XYL
	 * @DateTime 2018-06-08
	 * @return   [type]     [description]
	 */
	public function create_record()
	{
		$this->load->model('Step_model','step_mdl');
		$this->step_mdl->create_today_record();
	}

	/**
	 * 新增一个用户的数据
	 * @author XYL
	 * @DateTime 2018-06-08
	 * @param    [type]     $user_id [description]
	 * @return   [type]              [description]
	 */
	public function update_step($user_id)
	{
		$this->load->model('Step_model','step_mdl');
		$this->step_mdl->update_user_step($user_id,mt_rand(1, 3000));
	}
	/**
	 * 跳转到登录页面
	 * @DateTime 2017-08-17
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function jumpLogin()
	{
		$this->load->view('App/common/jump');
	}
	/**
	 * 获取签名(测试)
	 * @DateTime 2017-07-27
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function getSign(){
		$post = file_get_contents("php://input");
		$post = json_decode($post,true);

        // ksort($post); //键值升序
		$str = '';
		foreach($post as $key=>$val){
			if(!is_array($val) && $key!='sign'){
				$str.=$key.'='.$val.'&';
			}
		}
        // print_r($str.'key='.APP_KEY);die();
		$sign = md5($str.'key='.APP_KEY);

		print_r($sign);
	}
	
	function wxindex(){
		$this->load->view('weixin/index');
	}
}